package com.airnews.ticket.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Environment;
import android.text.TextUtils;

/**
 * 
 * 
 * @author luoxz
 */
public class ImageUtils {
	
	
	/**
	 * ��Bitmapָ����λ����д����
	 * @param bitmap
	 * @param text
	 * @param x
	 * @param y
	 * @param color #RGB
	 * @return ��д���ֵ���Bitmap
	 */
	public static Bitmap drawTextAtBitmap(Bitmap bitmap, ArrayList<DrawTextParam> paramList) {

		// ����һ����ԭͼͬ���С��λͼ
		Bitmap newbit = Bitmap.createBitmap(bitmap.getWidth(), bitmap
				.getHeight(), Bitmap.Config.ARGB_8888);

		Canvas canvas = new Canvas(newbit);

		Paint paint = new Paint();

		// ��ԭʼλ��0��0����ԭͼ
		canvas.drawBitmap(bitmap, 0, 0, paint);

		for(DrawTextParam param : paramList){
			if(!TextUtils.isEmpty(param.text)){
				paint.setColor(Color.parseColor(param.color));
				paint.setTextSize(param.textSize);
				// ��ԭͼָ��λ��д����
				canvas.drawText(param.text, param.x, param.y, paint);
			}
		}
		
		canvas.save(Canvas.ALL_SAVE_FLAG);

		// �洢
		canvas.restore();

		return newbit;
	}

	
	/**
	 * ����Bitmap
	 * @param bitmap
	 * @param fileName
	 * @return ����ɹ�ʱ���ر���·�������򷵻�null(����ʧ�ܣ�ԭ�������SD��û�в�û�û��д��Ȩ��)
	 */
	public static String saveBitmap(Bitmap bitmap, String fileName) {
		if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
			String path = Environment.getExternalStorageDirectory() + "/" + fileName;
			try {
				File f = new File(path);
				f.createNewFile();
				FileOutputStream fOut = new FileOutputStream(f);
				bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
				fOut.flush();
				fOut.close();
				return path;
			} catch (IOException e) {
			}
		}
		return null;
	}

	
	/**
	 * ���ź���תͼƬ
	 * @param bitmap
	 * @param newWidth ���
	 * @param newHeight �߶�
	 * @param rotate ��ת��
	 * @return ���ź���ת�����Bitmap
	 */
	public static Bitmap risizeBitmap(Bitmap bitmap, int newWidth, int newHeight, int rotate) {
		// ��ȡ���ͼƬ�Ŀ�͸�
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();

		// ���������ʣ��³ߴ��ԭʼ�ߴ�
		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;

		// ��������ͼƬ�õ�matrix����
		Matrix matrix = new Matrix();

		// ����ͼƬ����
		matrix.postScale(scaleWidth, scaleHeight);

		// ��תͼƬ ����
		matrix.postRotate(rotate);

		// �����µ�ͼƬ
		Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width,
				height, matrix, true);
		return resizedBitmap;
	}
	
	
	
	/**
	 * ��д���ֵĲ����װ��
	 */
	public static class DrawTextParam {
		
		public DrawTextParam(){};
		
		/**
		 * @param text
		 * @param x
		 * @param y
		 * @param textSize
		 * @param color
		 */
		public DrawTextParam(String text, int x, int y,
				int textSize, String color) {
			this.text = text;
			this.x = x;
			this.y = y;
			this.textSize = textSize;
			this.color = color;
		}

		public String text;
		
		public int x;
		
		public int y;
		
		public int textSize;
		
		public String color;
		
	}
	
}
